Procedure and function overloading
This is a New feature of Delphi 4. Like C++ function overloading now in Delphi you
can write different versions of function or procedure with different parameters type
and result but with the same name such as:
function Sum(X, Y: integer): integer; overload;
begin
Result:= X + Y;
end;
function Sum(X, Y: Double): Double; overload;
begin
Result:= X + Y;
end;
If you call Sum function with integer parameters first function will be invoked
such as:
I:= Sum(4, 5);
If you call Sum function with Double parameters second function will be invoked
such as:
F:= Sum(1.2, 576.124);
Example:
function Sum(X, Y: integer): integer; overload;
begin
Result:= X + Y;
end;
function Sum(X, Y: Double): Double; overload;
begin
Result:= X + Y;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i1, i2: integer;
f1, f2: Double;
begin
i1:= 10;
i2:= 20;
ShowMessage(IntToStr(Sum(i1, i2)));
f1:= 1.1;
f2:= 2.2;
ShowMessage(FloatToStr(Sum(f1, f2)));
end;
Note
You must include Overload directive at the end of function or procedure declaration.
See also:
Default parameters